﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Studio77.GuessTheWords2.Model;
using Studio77.GuessTheWords2.Core;
using Studio77.GuessTheWords2.Entities;

namespace Studio77.GuessTheWords2.Pages
{
    public partial class StagePage : PhoneApplicationPage
    {
        public static StagePage Instance = null;

        public StagePage()
        {
            InitializeComponent();
            Instance = this;
            for (int i = 0; i < GameStatics.StageCount; i++)
            {
                stages.Add(new StageInfoViewModel() { Index = i + 1 });
            }
            this.lstStages.DataContext = stages;
        }

        StageInfoCollection stages = new StageInfoCollection();

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            this.txtClassIndex.Text = StageManager.Instance.CurrentClass.ToString();

            for (int i = 0; i < GameStatics.StageCount; i++)
            {
                string key = StageManager.Instance.CurrentClass + "_" + (i + 1);
                StageData sd = StageManager.Instance.StageDatas[key];
                StageInfoViewModel model = stages[i];
                if (sd.AllWordCount > 0)
                {
                    model.Score = sd.Score;
                    model.GameTime = (sd.TotalSeconds / 60).ToString() + "'" + (sd.TotalSeconds % 60);
                    model.CompleteCount = sd.ComplateCount.ToString() + "/" + sd.AllWordCount.ToString();

                    double rate = sd.ComplateCount / (float)sd.AllWordCount;
                    if (rate < 0.1f)
                    {
                        model.NoStarVisibility = System.Windows.Visibility.Visible;
                        model.OneStarVisibility = System.Windows.Visibility.Collapsed;
                        model.TwoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.ThreeStarVisibility = System.Windows.Visibility.Collapsed;
                    }
                    else if (rate < 0.4f)
                    {
                        model.NoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.OneStarVisibility = System.Windows.Visibility.Visible;
                        model.TwoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.ThreeStarVisibility = System.Windows.Visibility.Collapsed;
                    }
                    else if (rate < 0.8f)
                    {
                        model.NoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.OneStarVisibility = System.Windows.Visibility.Collapsed;
                        model.TwoStarVisibility = System.Windows.Visibility.Visible;
                        model.ThreeStarVisibility = System.Windows.Visibility.Collapsed;
                    }
                    else
                    {
                        model.NoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.OneStarVisibility = System.Windows.Visibility.Collapsed;
                        model.TwoStarVisibility = System.Windows.Visibility.Collapsed;
                        model.ThreeStarVisibility = System.Windows.Visibility.Visible;
                    }
                }
                else
                {
                    model.LockVisibility = System.Windows.Visibility.Visible;
                }
            }

            this.lstStages.DataContext = stages;
        }

    }
}